using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using CE.Global;

public partial class controls_usrJobNature : System.Web.UI.UserControl
{
    protected string strJobNature = "Select";
    protected string strValidatinoGroup = "";
    protected string strErrorMessage = "Choose nob nature";
    protected bool bEnabled = true;
    bool bReadOnly = false;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //DataTable dtJobNature = General.GetComboItem("TH.JobNature", false);
            //ddlJobNature.DataSource = dtJobNature;
            //ddlJobNature.DataTextField = dtJobNature.Columns[0].ColumnName.ToString();
            //ddlJobNature.DataValueField = dtJobNature.Columns[1].ColumnName.ToString();
            //ddlJobNature.DataBind();
            //ddlJobNature.Items.Insert(0, new ListItem("Select", "-1"));

            //ddlJobNature.Attributes.Add("onblur", "ShowValidationEffect('" + ddlJobNature.ClientID + "','" + rfvJobNature.ClientID + "');");
        }
    }

    public void ChangeValidationEffectServerSide(string strValidationGroup)
    {
        if (rfvJobNature.Enabled && rfvJobNature.ValidationGroup == strValidationGroup)
        {
            rfvJobNature.Validate();
            if (rfvJobNature.IsValid == false)
            {
                ddlJobNature.ControlStyle.CssClass = "validationError";
            }
            else
            {
                ddlJobNature.ControlStyle.CssClass = "";
            }
        }
        else
        {
            ddlJobNature.ControlStyle.CssClass = "";
        }
    }

    //protected void ServerValidation(object source, ServerValidateEventArgs args)
    //{
    //    try
    //    {
    //        // Test whether the value is -1
    //        if (args.Value != "-1")
    //        {
    //            args.IsValid = true;
    //        }
    //        else
    //        {
    //            args.IsValid = false;
    //        }
    //    }

    //    catch (Exception ex)
    //    {
    //        args.IsValid = false;
    //    }
    //}

    public bool IsEnabled
    {
        get { return bEnabled; }
        set
        {
            bEnabled = value;
            SetEnabled();
        }
    }

    public bool IsReadOnly
    {
        get { return bReadOnly; }
        set
        {
            bReadOnly = value;
            //SetReadOnly();
        }
    }

    public void IsEnableValidation(bool bValue)
    {
        rfvJobNature.Enabled = bValue;
    }

    public string JobNature
    {
        get { return strJobNature; }
        set { strJobNature = value; }
    }

    public string ValidationGroup
    {
        get { return strValidatinoGroup; }
        set { strValidatinoGroup = value; }
    }

    public string ErrorMessage
    {
        get { return strErrorMessage; }
        set { strErrorMessage = value; }
    }

    private void SetEnabled()
    {
        //ddlJobNature.SelectedIndex = 0;
        ddlJobNature.Enabled = bEnabled;
        rfvJobNature.Enabled = bEnabled;
    }

    protected override void OnPreRender(EventArgs e)
    {
        SetEnabled();

        if (strJobNature != string.Empty)
        {
            if (strJobNature != "Select")
                ddlJobNature.Text = strJobNature;
        }

        if (strValidatinoGroup != null)
        {
            if (strValidatinoGroup != string.Empty)
                rfvJobNature.ValidationGroup = strValidatinoGroup;
        }

        if (bReadOnly)
        {
            SetReadOnly();
        }

        if (strErrorMessage != string.Empty)
        {
            //rfvJobNature.ErrorMessage = strErrorMessage;
        }
    }

    private void SetReadOnly()
    {
        if (bReadOnly)
        {
            ddlJobNature.Text = strJobNature;

            string orgJobNatureText = ddlJobNature.SelectedItem.Text;
            string orgJobNatureValue = ddlJobNature.SelectedItem.Value;

            ddlJobNature.Items.Clear();

            ddlJobNature.Items.Add(new ListItem(orgJobNatureText, orgJobNatureValue));
        }
    }

    public string GetJobNature()
    {
        strJobNature = ddlJobNature.SelectedValue;
        return strJobNature;
    }
}
